package user

import (
	"strconv"

	"gitcode.com/G-YT/baoyou/server/global"
	"gitcode.com/G-YT/baoyou/server/model/common/response"
	"gitcode.com/G-YT/baoyou/server/model/user"
	userReq "gitcode.com/G-YT/baoyou/server/model/user/request"
	"gitcode.com/G-YT/baoyou/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type BindApi struct {
}

// BindManager 绑定健管师（用户端）
// @Tags UserBindManage
// @Summary 绑定健管师（用户端）
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body user.UserBindManager true "健管师ID"
// @Success 200 {string} string "{\"success\":true,\"data\":{},\"msg\":\"提交成功\"}"
// @Router /bind/BindManager [post]
func (bindApi *BindApi) BindManager(c *gin.Context) {
	var bind user.BindRecord
	if err := c.ShouldBindJSON(&bind); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	uid := utils.GetUserID(c)
	// 获取当前用户信息，判断是否已绑定健管师
	userInfo, err := UserService.GetUserData(uid)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if userInfo.HealthManagerID != 0 {
		response.FailWithMessage("请先解除绑定您的健管师", c)
		return
	}
	bind.UserID = uid
	bind.Status = 1
	if err = bindService.BindManager(&bind); err != nil {
		global.GVA_LOG.Error("提交失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithMessage("提交成功", c)
}

// UnBindManager 解绑健管师（用户端）
// @Tags UserBindManage
// @Summary 解绑健管师（用户端）
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{\"success\":true,\"data\":{},\"msg\":\"解绑成功\"}"
// @Router /bind/unBindManager [put]
func (bindApi *BindApi) UnBindManager(c *gin.Context) {
	uid := utils.GetUserID(c)
	userInfo, err := UserService.GetUserData(uid)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if userInfo.HealthManagerID == 0 {
		response.FailWithMessage("您还没有健管师，请尽快绑定", c)
		return
	}
	if err = bindService.UnBindManager(uid); err != nil {
		global.GVA_LOG.Error("解绑失败!", zap.Error(err))
		response.FailWithMessage("解绑失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("解绑成功", c)
}

// GetBindList 获取绑定用户列表
// @Tags UserBindManage
// @Summary 获取绑定用户列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query userReq.BindRecordSearch true "获取绑定用户列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /bind/getBindList [get]
func (bindApi *BindApi) GetBindList(c *gin.Context) {
	var pageInfo userReq.BindRecordSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := bindService.GetBindInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// UpdateBindStatus 更改绑定状态
// @Tags UserBindManage
// @Summary 更改绑定状态
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body user.UserBindManage true "更改绑定状态"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /Bind/updateBindStatus [put]
func (bindApi *BindApi) UpdateBindStatus(c *gin.Context) {
	var Bind user.BindRecord
	err := c.ShouldBindJSON(&Bind)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := bindService.UpdateBindStatus(Bind.ID, strconv.Itoa(int(Bind.Status))); err != nil {
		global.GVA_LOG.Error("操作失败!", zap.Error(err))
		response.FailWithMessage("操作失败", c)
	} else {
		response.OkWithMessage("操作成功", c)
	}
}

// FindBind 用id查询用户绑定健管师
// @Tags UserBindManage
// @Summary 用id查询用户绑定健管师
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query user.UserBindManage true "用id查询用户绑定健管师"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /Bind/findBind [get]
func (bindApi *BindApi) FindBind(c *gin.Context) {
	id := c.Query("ID")
	if reBind, err := bindService.GetBind(id); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reBind": reBind}, c)
	}
}

// GetUserBindManageList 分页获取用户绑定健管师列表
// @Tags UserBindManage
// @Summary 分页获取用户绑定健管师列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query userReq.BindRecordSearch true "分页获取用户绑定健管师列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /Bind/getUserBindManageList [get]
func (bindApi *BindApi) GetUserBindManageList(c *gin.Context) {
	var pageInfo userReq.BindRecordSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := bindService.GetUserBindManageInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
